package com.hm.health.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.view.WindowManager;

import com.hm.health.R;

import java.util.regex.Pattern;

/**
 * Created by chenshi on 2016/11/18.
 */
public class CommonUtils {

    private static final String TAG = "CommUtils";

    public static int getSystemWidth(Context context) {
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    public static int getSystemHeight(Context context) {
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    public static String getAppVersionName(Context c) {
        try {
            PackageInfo info = c.getPackageManager().getPackageInfo(c.getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return c.getResources().getString(R.string.default_version_name);
        }
    }

    /**
     *
     * @param activity
     * @param cls
     */
    public static void startActivity(Activity activity, Class<?> cls) {
        Intent intentTestCondition = new Intent(activity, cls);
        activity.startActivity(intentTestCondition);
    }


    public static String intArr2String(int[] dataArr) {
        if(null == dataArr ||  dataArr.length == 0){
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < dataArr.length - 1; ++i) {
            buffer.append("" + dataArr[i] + ",");
        }

        buffer.append("" + dataArr[dataArr.length - 1]);
        return buffer.toString();
    }

    public static String intArr2String(int[] dataArr, String info) {
        if(null == dataArr ||  dataArr.length == 0){
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < dataArr.length - 1; ++i) {
            buffer.append("" + dataArr[i] + ",");
        }

        buffer.append("" + dataArr[dataArr.length - 1] + "," + info + "\n");
        return buffer.toString();
    }

    public static String StringArr2String(String[] dataArr) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < dataArr.length - 1; ++i) {
            buffer.append("" + dataArr[i] + ",");
        }

        buffer.append("" + dataArr[dataArr.length - 1]);
        return buffer.toString();
    }

    /**
     * Encryption data by XOR_PARAM
     * @param data
     * @param nFrom
     * @param nTo
     */
    private static final int[] XOR_PARAM = new int[]{0x9d8f, 0x4d62, 0xb9de, 0xe342, 0xbfa5, 0x6ce, 0xd53e, 0xde04};
    public static void encryption(int[] data, int nFrom, int nTo) {
        if((null == data) || (0 == data.length)) {
            return;
        }

        int nCount = 0;
        for (int i = nFrom; i < nTo; ++i) {
            data[i] = (data[i] ^ XOR_PARAM[nCount % XOR_PARAM.length]);
            nCount++;
        }
    }

    public static boolean isNumber(String string) {
        if (string == null)
            return false;
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        return pattern.matcher(string).matches();
    }

    public static void runOnUIThread(Runnable runnable) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            runnable.run();
        } else {
            new Handler(Looper.getMainLooper()).post(runnable);
        }
    }
}
